package org.i1510.common.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.i1510.common.bean.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Service;

import com.ibatis.sqlmap.client.SqlMapClient;



/*
 * 基本框架 带有树结构的service
 * docId 必填
 * docSubject 必填
 * pid 必填
 * pidName 必填
 * */

public class BaseTreeServiceImpl {



	/**
	 * 以私有成员变量保存SqlMapClient
	 */
	@Autowired
	private SqlMapClient sqlMapClient;//需要有set方法注入


	

	/**
	 * 以私有成员变量保存iBatis模板SqlMapClientTemplate
	 */
	
	private SqlMapClientTemplate sqlMapClientTemplate;
	/**
	 * 获取iBatis模板对象
	 * 
	 * @return SqlMapClientTemplate iBatis模板对象
	 */
	public SqlMapClientTemplate getSqlMapClientTemplate() {
		//首先检查原来的SqlMapClientTemplate实例是否存在
		if (this.sqlMapClientTemplate == null) {
			//如果不存在，则创建一个SqlMapClientTemplate实例
			this.sqlMapClientTemplate = new SqlMapClientTemplate(
					this.sqlMapClient);
		}
		return this.sqlMapClientTemplate;
	}
	
/*
 * tree 公有方法 Start
 * */

    /**
	*@Author Rambo
	*@Desc：通过递归生成tree结构 
	*@param id
	*@param childrenlist
	*@param pidName 父节点Name
	*@return TODO
	*@Version  SysUserServiceImpl.java,v 1.1 2016年7月5日 下午11:33:20 
	*/
	public List<Map> getTree(String id,List childrenlist,String pidName) {
		List resultlist = new ArrayList();

		//当前级菜单集合
		List list = this.getChildrens(childrenlist, id,pidName);
		for (Object obj : list) {
			Map map = (Map) obj;

			TreeNode treeNode = new TreeNode();
			treeNode.setId(map.get("docId").toString());
			treeNode.setText(map.get("docSubject").toString());
			//默认的menu-folder图标
			if(null != map.get("icon") && !map.get("icon").equals("")){
				treeNode.setIconCls(map.get("icon").toString());
			}else{
				treeNode.setIconCls("menu-folder");
			}
			//url
			if(null != map.get("url") && !map.get("url").equals("")){
				treeNode.setUrl(map.get("url").toString());
			}
			
			//子菜单
			List children = this.getChildrens(childrenlist, map.get("docId").toString(),pidName);
			if (children.size() > 0) {//有子类别集合
				treeNode.setLeaf(false);
				treeNode.setChildren(getTree(map.get("docId").toString(),childrenlist,pidName)); //递归调   
			} else {//该节点为叶子    
				treeNode.setLeaf(true);
			}
			resultlist.add(treeNode);
		}
		return resultlist;
	}

	/**
	*@Author Rambo
	*@Desc：从集合中找出父节点id为pid的类别集合
	*@param list
	*@param pid 父节点id
	*@param pidName 父节点Name
	*@return TODO
	*@Version  SysUserServiceImpl.java,v 1.1 2016年7月5日 下午11:33:39 
	*/
	public List getChildrens(List list, String pid,String pidName) {
		List resultList = new ArrayList();
		Map map = null;
		for (Object obj : list) {
			map = (Map) obj;
			if (map.get(pidName).toString()!=null && map.get(pidName).toString().equals(pid)) {//父节点id
				resultList.add(map);
			}
		}
		return resultList;
	}
	
/*
 * tree 公有方法 End
 * */	
	
	
	public Object insertBySql(String statementId, Object value) {
		return this.getSqlMapClientTemplate().insert(statementId, value);
	}

	public int deleteBySql(String statementId, Object value) {
		return this.getSqlMapClientTemplate().delete(statementId, value);
	}

	
	public int updateBySql(String statementId, Object value) {
		return this.getSqlMapClientTemplate().update(statementId, value);
	}

	public List queryForListBySql(String statementId, Object value) {
		List list = this.getSqlMapClientTemplate().queryForList(statementId,
				value);
		return list;
	}




}
